package com.yonyou.iuap.jxzy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.microsoft.schemas.office.visio.x2012.main.PageType;
import com.yonyou.iuap.base.web.BaseController;
import com.yonyou.iuap.jxzy.dao.WastePaperHeadMapper;
import com.yonyou.iuap.jxzy.entity.MonitorSample;
import com.yonyou.iuap.jxzy.entity.WastePaperBodyVO;
import com.yonyou.iuap.jxzy.entity.WastePaperHeadVO;
import com.yonyou.iuap.jxzy.service.MonitorSampleService;
import com.yonyou.iuap.jxzy.service.WastePaperBodyService;
import com.yonyou.iuap.jxzy.service.WastePaperHeadService;
import com.yonyou.iuap.mvc.type.JsonResponse;
import com.yonyou.iuap.mvc.type.SearchParams;
import com.yonyou.iuap.persistence.bs.dao.BaseDAO;

/**
 * app进口废纸保存、检测查询、称重查询控制类
 * @author wanggep
 * @date 2019年5月28日
 */
@Controller
@RequestMapping(value="/jx_importcontroller")
public class ImportWastePaperController extends BaseController{

	private Logger logger= LoggerFactory.getLogger(ImportWastePaperController.class);

	private WastePaperHeadService wasteheadservice; 

	private WastePaperBodyService wastebodyservice;


	@Autowired
	public void setWastePaperHeadService(WastePaperHeadService wasteheadservice) {
		this.wasteheadservice = wasteheadservice;
	}

	@Autowired
	public void setWastePaperBodyService(WastePaperBodyService wastebodyservice) {
		this.wastebodyservice = wastebodyservice;
	}

	
	/**
	 * 保存app进口废纸
	 * @author wanggp
	 */
	@RequestMapping(value="/saveImportWastePaper")
	@ResponseBody
	public Object saveImportWastePaper(@RequestBody WastePaperHeadVO headvo, 
			HttpServletRequest request,HttpServletResponse response){
		JSONObject jsonObject=new JSONObject();
		String mess=null;
		try {
			mess=wasteheadservice.insertPaperHead(headvo,headvo.getBodylist());
			return mess;
		} catch (Exception e) {
			jsonObject.put("message", "新增进口废纸检测异常："+e.getMessage());
			jsonObject.put("success", "error");
		}
		return jsonObject.toString();
	}
	
	/**
	 * 查询app进口废纸
	 * @author wanggp
	 */
	@RequestMapping(value="/queryImportWastePaper")
	@ResponseBody
	public Object queryImportWastePaper(PageRequest pageRequest, SearchParams searchParams,HttpServletRequest request,HttpServletResponse response){
		JSONObject jsonObject=new JSONObject();
		Map<String, Object> map=searchParams.getSearchMap();
		List<WastePaperHeadVO> headvolist=null;
		try {
			headvolist=this.wasteheadservice.queryPaperHeadVO(map);
			for(WastePaperHeadVO headvo:headvolist){
				String pk_pound=headvo.getPk_pound();
				String imgurl=headvo.getImgUrls();
				if(imgurl!=null){
					String[] imgurls=imgurl.split(",");
					if(map.get("ismobile")!=null && map.get("ismobile")!="" 
							&& "Y".equals(map.get("ismobile"))){//app传入
						JSONArray jsarry=new JSONArray();
						for(int i=0;i<imgurls.length;i++){
							JSONObject jsobt=new JSONObject();
							jsobt.put("jid", i);
							jsobt.put("path", imgurls[i]);
							jsarry.add(jsobt);
						}
						headvo.setImgUrls(jsarry.toJSONString());
					}else{//iuap需要的图片格式
						headvo.setImgUrl(imgurls);
					}
				}
				List<WastePaperBodyVO> bodyvolist=null;
				if(pk_pound!=null){
					bodyvolist=this.wastebodyservice.queryPaperBodyVO(pk_pound);
				}
				headvo.setBodylist(bodyvolist);
			}
		} catch (Exception e) {
			jsonObject.put("message", "查询进口废纸接口异常:"+e.getMessage());
			jsonObject.put("success", "error");
			return jsonObject.toString();
		}
		return this.buildSuccess(headvolist);
	}
}
